/* PhotoImport
 * Main class for program entry
 * Copyright by faena.com
 * 2010-08-17 VM created
 * 
*/
using System;
using Gtk;

namespace PhotoImport
{
	class MainClass
	{
		public static void Main (string[] args)
		{
			//I'm not sure what is this for
			Application.Init ();
			
			//Create a new log file. If file exists, clear it.
			LogStart();
			
			try
			{
				//This is our main window
				MainWindow win = new MainWindow ();
				
				//if the location is passed in when program starts, use it as source location 
				if (args.Length > 0)
					win.SourceDir = args[0];
				
				//Present our mian window
				win.Show();
				
				//Run our application and wait for it quits
				Application.Run ();
			
			}
			catch(Exception ex)
			{
				Log(ex.ToString());
			}
			//When application quits, put an entry in the log file
			LogStop ();
		}
		
		private static void Log(string pMessage)
		{
			string logFile = Environment.GetFolderPath (Environment.SpecialFolder.Personal);
			logFile = System.IO.Path.Combine(logFile, "PhotoImport.log");
			using (System.IO.StreamWriter sw = new System.IO.StreamWriter(new System.IO.FileStream(logFile, System.IO.FileMode.Append, System.IO.FileAccess.Write, System.IO.FileShare.ReadWrite)))
			{
				sw.WriteLine(DateTime.Now.ToString("u") + "\t" + pMessage);
				sw.Close();
			}
		}
		/// <summary>
		/// Put last entry in the log file
		/// </summary>
		private static void LogStop ()
		{
			string message = DateTime.Now.ToString("u") + "\tProgram stop.";
			string logFile = Environment.GetFolderPath (Environment.SpecialFolder.Personal);
			logFile = System.IO.Path.Combine(logFile, "PhotoImport.log");
			using (System.IO.StreamWriter sw = new System.IO.StreamWriter(new System.IO.FileStream(logFile, System.IO.FileMode.Append, System.IO.FileAccess.Write, System.IO.FileShare.ReadWrite)))
			{
				sw.WriteLine(message);
				sw.Close();
			}
		}

		/// <summary>
		/// Put first entry in the log file
		/// </summary>
		private static void LogStart()
		{
			string message = DateTime.Now.ToString("u") + "\tProgram start.";
			string logFile = Environment.GetFolderPath (Environment.SpecialFolder.Personal);
			logFile = System.IO.Path.Combine(logFile, "PhotoImport.log");
			using (System.IO.StreamWriter sw = new System.IO.StreamWriter(new System.IO.FileStream(logFile, System.IO.FileMode.Create, System.IO.FileAccess.Write, System.IO.FileShare.ReadWrite)))
			{
				sw.WriteLine(message);
				sw.Close();
			}
		}
	}
}

